package com.marquisx.common.ui;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.marquisx.tzdice.R;
import com.marquisx.tzdice.Setting;

/**
 * A grid that displays a set of framed photos.
 */
public class PictureGrid extends Activity implements OnClickListener
{
	protected Context mContext;
	
	/** must be override by its sub class */
	protected Integer[] mThumbIds;
	/** must be override by its sub class */
	protected Integer[] mThumbIds_pro;

	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		setContentView(R.layout.user_profile_grid);

		GridView g = (GridView) findViewById(R.id.user_profile_grid);
		g.setAdapter(new ImageAdapter(this));
	}

	@Override
	public void onClick(View v)
	{
		int picId = Integer.parseInt((String)v.getTag());
		
		Setting.setUserProfilePicId(this, picId);
		
		finish();
	}

	public class ImageAdapter extends BaseAdapter
	{
		public ImageAdapter(Context c)
		{
			mContext = c;
		}

		public int getCount()
		{
			return mThumbIds.length;
		}

		public Object getItem(int position)
		{
			return position;
		}

		public long getItemId(int position)
		{
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent)
		{
			ImageView imageView;
			if (convertView == null)
			{
				imageView = new ImageView(mContext);
				imageView.setLayoutParams(
						new GridView.LayoutParams(
							ViewGroup.LayoutParams.WRAP_CONTENT, 
							ViewGroup.LayoutParams.WRAP_CONTENT)
				);
			}
			else
			{
				imageView = (ImageView) convertView;
			}

			if(getResources().getBoolean(R.bool.app_version_pro))
			{
				imageView.setImageResource(mThumbIds_pro[position]);
				imageView.setTag(mThumbIds_pro[position] + "");
			}
			else
			{
				imageView.setImageResource(mThumbIds[position]);
				imageView.setTag(mThumbIds[position] + "");
			}

			imageView.setOnClickListener(PictureGrid.this);
			return imageView;
		}
	}
}